<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
    <link rel="stylesheet" href="./css/registry.css">
</head>
<body>
    <div class="login-container">
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span><input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span><input type="password" id="password">
            </div>
            <div class="row">
                <span>确认密码</span><input type="password" id="repassword">
            </div>
            <div class="row">
                <span>头像</span><input type="file" id="file">
            </div>
            <div class="btn">
                <button>注册</button>
            </div>
        </div>
    </div>

    <script src="./js/jquery.min.js"></script>
    <script>
        let registrBtn = document.querySelector('.login-dialog .btn button')
        registrBtn.onclick = function() {
            let username = document.querySelector('.login-dialog #username').value
            let password = document.querySelector('.login-dialog #password').value
            let repassword = document.querySelector('.login-dialog #repassword').value
            let file = document.querySelector('.login-dialog #file').files[0]
            console.log(file)
            if (password != repassword) {
                alert('两次输入的密码不一致')
                return
            }
            let formData = new FormData();  
            formData.append('username', username);  
            formData.append('password', password);  
            formData.append('repassword',repassword)
            // 如果用户没有选择文件，则不发送file字段  
            if (file) {  
                formData.append('file', file);  
            }
            $.ajax({
                method: 'post',
                url: '/user/registry',
                // 必须设置这两个选项  
                processData: false, // 告诉jQuery不要处理发送的数据  
                contentType: false,  // 告诉jQuery不要设置Content-Type请求头 
                data: formData,
                success: function(body) {
                    if (body != null && body.status == 200) {
                        if (body.data == true) {
                            alert('注册成功')
                            location.href = 'login.html'
                        }else {
                            alert(body.message)
                        }
                    }else {
                        alert(body.message)
                    }
                },
                error: function(body) {
                    alert('内部出现错误，请联系管理员')
                }
            });
        }
    </script>
</body>
</html>